using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Objects
{
    public class BagSystem : Singleton<BagSystem>
    {
        private Dictionary<int, object> contentID = new Dictionary<int, object>();
        /// <summary>
        /// 加入纯ID物品()
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ID"></param>
        public void AddObjectInBag(int ID)
        {
            contentID.Add(ID, null);
        }
        public void AddObjectInBag<T>(T objectClass, bool transParent = false) where T : ObjectBase, CouldGet
        {
            if (transParent)
                objectClass.TransParant(GameManager.Instance.GetBigSlime());
            CouldGet get = objectClass.GetComponent<CouldGet>();
            if (get != null)
            {
                if (!contentID.ContainsKey(objectClass.ID))
                {
                    contentID.Add(objectClass.ID, objectClass);
                    return;
                }
                else Debug.LogWarning("拾取了ID重复的物品");
            }
        }
        public T FindObjectInBag<T>(int ID) where T : ObjectBase, CouldGet
        {
            if (contentID.ContainsKey(ID)) return (T)contentID[ID];
            Debug.Log("背包不存在此物体");
            return default(T);
        }
        /// <summary>
        /// 从背包中找到物品
        /// </summary>
        /// <typeparam name="T">返回物品的类</typeparam>
        /// <param name="ID">ID</param>
        /// <param name="delete">是否删除背包内的物品</param>
        /// <returns></returns>
        public T FindObjectInBag<T>(int ID, bool delete = false) where T : ObjectBase, CouldGet
        {
            if (contentID.ContainsKey(ID))
            {
                T data = (T)contentID[ID];
                if (delete) contentID.Remove(ID);
                return data;
            }
            Debug.Log("背包不存在此物体");
            return default(T);
        }
        /// <summary>
        /// 确定是否包含特点的Id的物品
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public bool ContentObject(int ID)
        {
            if (contentID.ContainsKey(ID)) return true;
            return false;
        }
        public CouldThrow GetThrowObject()
        {
            foreach (int data in contentID.Keys)
            {
                if (contentID[data] == null) continue;
                object a = contentID[data];
                Type myType = a.GetType();
                Type dataType = myType.GetInterface(typeof(CouldThrow).Name);
                if (dataType != null)
                {
                    contentID.Remove(data);
                    return (CouldThrow)a;
                }
            }
            return null;
        }
    }
}